<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>取消定时器</button>
    <script>
        /* 
           取消定时器需要基于setTimeout函数的返回值。
        */
        const btnObj = document.querySelector("button");
        const timeout1 = setTimeout(function () {
            console.log("3秒后执行该函数");
        }, 3000);

        btnObj.onclick = function () {
            clearTimeout(timeout1);
        }
    </script>
</body>

</html>